from openpyxl import load_workbook, Workbook
import re

v = '(互联网事业部)智慧健康通V3.5(工时：8.0) exchange领域编排 抗菌药bug修改 '
order_list = ('一.', '二.', '三.', '四.', '五.')
regex = r"\(.*\)"


def parse_fragment(f: str):
    if '工时：' in f:
        index = f.index('工时：')
        end = f.index(')', index)

        return (f[0:index-1], f[index+3:end])
    else:
        return None


def parse_line(s: str, list_index=0, up_result=None):
    result = up_result if up_result is not None else []
    if order_list[list_index] in s:
        if list_index+1 >= len(order_list):
            fragment = s[2:]
            result.append(parse_fragment(fragment))

        elif order_list[list_index+1] in s:
            next_index = s.index(order_list[list_index+1])
            fragment = s[2:next_index-1]

            result.append(parse_fragment(fragment))

            new_str = s[next_index:]
            parse_line(new_str, list_index+1, result)

        else:
            fragment = s[2:]
            result.append(parse_fragment(fragment))



    else:
        s = parse_fragment(s)
        result.append(s)

    return result


def main():
    wb = load_workbook(filename='D:\\opt\\tmp\\121212.xlsx')
    new_wb = Workbook()
    new_sheet = new_wb.active
    #new_sheet = new_wb.create_sheet('121212')

    sheet1 = wb['121212']
    size = sheet1.max_row
    index = 2
    write_index =1
    while index < size:
        v = sheet1['D'+str(index)].value
        if v is not None and len(v) >0:
            r = parse_line(v)

            for f in r:
                new_sheet['A'+str(write_index)].value = sheet1['A'+str(index)].value
                new_sheet['B'+str(write_index)].value = sheet1['B'+str(index)].value
                new_sheet['C'+str(write_index)].value = sheet1['C'+str(index)].value
                if f is not None and len(f) > 0:
                    new_sheet['D'+str(write_index)] = f[0]
                    if 'h' in f[1]:
                        v = f[1].replace('h','')
                        new_sheet['E'+str(write_index)].value =float(v)

                    else:
                        new_sheet['E'+str(write_index)].value = float(f[1])
                else:
                    pass
                write_index+=1

            print(r)
        index = index+1

    new_wb.save("a.xlsx")

def regex_test():
    v = '(互联网事业部)智慧健康通V3.5(工时：8.0) exchange领域编排 抗菌药bug修改 '
    m = re.match(regex,v,flags=0)
    print(m)

if __name__ == '__main__':
    main()

